home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / memory / xms200je / xarrtime.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-22  |  5.3 KB  |  174 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      XARRTIME.CPP: timing test for XMS array class.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //--------------------------------------------------------------------------
  15. //
  16. //      Revision history:
  17. //      1.0     Jun 1993        Initial coding
  18. //      2.0     Nov 1993        Renamed from XMSBENCH to XARRTIME and
  19. //                              generally reorganised
  20. //                              
  21. //--------------------------------------------------------------------------
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <dos.h>
  26. #include "xmsarray.h"
  27. #include "xms.h"
  28.  
  29. //--------------------------------------------------------------------------
  30. //
  31. //      Global data.
  32. //
  33. const int lapse = 91;   // timer ticks per test (91 ticks = 5 seconds)
  34. volatile long far* timer = (volatile long far*) MK_FP(0x40,0x6C);
  35.                         // pointer to BIOS timer count
  36.  
  37.  
  38. //--------------------------------------------------------------------------
  39. //
  40. //      Display statistics.
  41. //
  42. void stats (int size, long loops)
  43. {
  44.     printf ("%9ld", loops);
  45.     printf ("%12.2f Mb/s\n", (size * loops) / (lapse * 1e6 / 18.2));
  46. }
  47.  
  48. //--------------------------------------------------------------------------
  49. //
  50. //      Test function.
  51. //
  52. //      This performs a timing test for an XMS array of a specified
  53. //      type with a specified number of elements and cache size for
  54. //      a specified number of iterations.
  55. //
  56. template<class T> void test (T* value, long elems, unsigned buff)
  57. {
  58.     long ticks;
  59.  
  60.     //--- allocate the array and report status
  61.     printf ("\nAllocation of %ld items of %u bytes each ...",
  62.             elems, sizeof(T)); fflush (stdout);
  63.     XMSarray<T> a(elems, buff);
  64.     printf ("\b\b\b%s\n%ld bytes of XMS free",
  65.             (a.valid() ? "succeeded" : "failed"),
  66.             XMS::available());
  67.     if (!a)
  68.     {   printf (".\n");
  69.         return;
  70.     }
  71.     printf (", buffer size = %u bytes.\nEach test takes 5 seconds.\n\n", buff);
  72.  
  73.     //--- display headings
  74.     printf ("             Items copied   Transfer rate\n");
  75.     long i;
  76.  
  77.     //--- write test: a[i] = value
  78.     printf ("Writing:     "); fflush (stdout);
  79.     ticks = *timer + lapse;
  80.     for (i = 0; *timer < ticks; i++)
  81.         a[i%elems] = *value;
  82.     stats (sizeof(T), i);
  83.  
  84.     //--- read test: a[i] = value
  85.     printf ("Reading:     "); fflush (stdout);
  86.     ticks = *timer + lapse;
  87.     for (i = 0; *timer < ticks; i++)
  88.         *value = a[i%elems];
  89.     stats (sizeof(T), i);
  90.  
  91.     //--- copy test 1: a[0] = a[0]
  92.     //    (Item copied from cache; gives raw cache speed)
  93.     printf ("Copying (1): "); fflush (stdout);
  94.     ticks = *timer + lapse;
  95.     for (i = 0; *timer < ticks; i++)
  96.         a[0] = a[0];
  97.     stats (sizeof(T), i);
  98.  
  99.     //--- copy test 2: a[0] = a[elems-1]
  100.     //    (Cache cruncher: items never cached, gives raw XMS speed)
  101.     printf ("Copying (2): "); fflush (stdout);
  102.     ticks = *timer + lapse;
  103.     for (i = 0; *timer < ticks; i++)
  104.         a[0] = a[elems - 1];
  105.     stats (sizeof(T), i);
  106. }
  107.  
  108. //--------------------------------------------------------------------------
  109. //
  110. //      Type definitions for array element types.
  111. //
  112. struct T1 { char x[10]; }    t1;
  113. struct T2 { char x[100]; }   t2;
  114. struct T3 { char x[1000]; }  t3;
  115. struct T4 { char x[10000]; } t4;
  116.  
  117.  
  118. //--------------------------------------------------------------------------
  119. //
  120. //      Main program.
  121. //
  122. void main ()
  123. {
  124.     printf ("XMS array timing test: %ld bytes of XMS free initially\n",
  125.             XMS::available());
  126.     for (;;)
  127.     {   printf ("\nChoose size of array items:\n"
  128.                 "  1) 10 bytes\n"
  129.                 "  2) 100 bytes\n"
  130.                 "  3) 1000 bytes\n"
  131.                 "  4) 10000 bytes\n"
  132.                 "  X) Exit program\n");
  133.         char buff [80];
  134.         for (;;)
  135.         {
  136.             printf ("Enter your choice: ");
  137.             fgets (buff, 80, stdin);
  138.             if ((buff[0] == 'x' || buff[0] == 'X') && buff[1] == '\n')
  139.                 exit (0);
  140.             if (buff[0] < '1' || buff[0] > '5' || buff[1] != '\n')
  141.                 printf ("Invalid choice!\a\n");
  142.             else
  143.                 break;
  144.         }
  145.  
  146.         printf ("Enter number of array elements: ");
  147.         long e;
  148.         scanf ("%ld", &e);
  149.         while (getchar() != '\n')
  150.             continue;
  151.         printf ("Enter size of internal buffer:  ");
  152.         unsigned b;
  153.         scanf ("%u", &b);
  154.         while (getchar() != '\n')
  155.             continue;
  156.  
  157.         switch (buff[0])
  158.         {
  159.           case '1':
  160.             test (&t1, e, b);
  161.             break;
  162.           case '2':
  163.             test (&t2, e, b);
  164.             break;
  165.           case '3':
  166.             test (&t3, e, b);
  167.             break;
  168.           case '4':
  169.             test (&t4, e, b);
  170.             break;
  171.         }
  172.     }
  173. }
  174.